package com.sinog.biz.service.business.impl;

import com.sinog.biz.mapper.havexml.PrisaveTempMapper;
import com.sinog.biz.mapper.havexml.PrisonerMainMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.mapper.noxml.PrisonerZzMapper;
import com.sinog.biz.service.business.MediaService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.PrisaveTemp;
import com.sinog.core.domain.PrisonerMain;
import com.sinog.core.domain.PrisonerZz;
import com.sinog.core.model.UploadAppendixFile;
import com.sinog.core.model.UserDTO;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;

/**
 * @desc 基本信息多媒体service
 * @author -
 * @date 2020-02-05-16:27
 */
@Service
public class MediaServiceImpl implements MediaService {

    @Resource
    private PrisaveTempMapper prisaveTempMapper;
    @Resource
    private PrisonerMainMapper prisonerMainMapper;
    @Resource
    private PrisonerZzMapper prisonerZzMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;

    /**
     * 保存罪犯多媒体信息
     * @param prinum prinum
     * @param id id
     * @param user user
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addMediaInfo(String prinum,String id,UserDTO user) {
        //1、处理PrisonerZz
        PrisonerZz prisonerZz = new PrisonerZz();
        // 先查看上传表中是否有头像信息 2
        UploadAppendixFile u1 = uploadAppendixFileMapper.getScInfoByPrinum(prinum,Constants.AppendixFile.APDIX_FILE_TWO);
        // 查看上传表中是否有指纹信息 3
        UploadAppendixFile u2 = uploadAppendixFileMapper.getScInfoByPrinum(prinum,Constants.AppendixFile.APDIX_FILE_THREE);
        if(ObjectUtils.allNull(u1,u2)) {
            return Result.failure(ReturnCode.SAVE_FAILED,"请上传信息");
        }
        PrisonerZz p = prisonerZzMapper.selectById(prinum);
        Date date = new Date();
        if(null == p) {
            prisonerZz.setId(IdGen.strid());
            prisonerZz.setCreateUser(user.getAccount());
            prisonerZz.setCreateTime(date);
            prisonerZz.setAdduserid(user.getDepartid());
        }
        prisonerZz.setUpdateUser(user.getAccount());
        prisonerZz.setUpdateTime(date);
        prisonerZz.setPriNum(prinum);
        boolean tag = prisonerZz.insertOrUpdate();
        // 2、更新中间表
        // 先判断中间表中是否有数据
        PrisaveTemp prisaveTemp = prisaveTempMapper.selectById(prinum);
        if(null == prisaveTemp) {
            prisaveTemp = new PrisaveTemp();
            prisaveTemp.setId(IdGen.strid());
        }
        prisaveTemp.setPriNum(prinum);
        prisaveTemp.setZzFlag(1);
        tag = tag && prisaveTemp.insertOrUpdate();
        //3、更新主表
        PrisonerMain pm = prisonerMainMapper.selectPrisonerMain(prinum);
        if(Constants.NumberStr.NUM_STR_ZERO.equals(pm.getState())) {
            tag = tag && updateState(prinum);
        }
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED,"保存失败");
    }

    /**
     * 更新主表状态,事务方法调用必须是pulic
     * @param prinum prinum
     * @return boolean
     */
    public boolean updateState(String prinum) {
        PrisaveTemp prisaveTemp = prisaveTempMapper.selectById(prinum);
        int row = -1;
        if(null != prisaveTemp) {
            int i = prisaveTemp.getMainFlag() + prisaveTemp.getFxFlag() + prisaveTemp.getSrFlag() + prisaveTemp.getZzFlag();
            if(Constants.Number.NUM_FOUR == i) {
                row = prisonerMainMapper.updateStateByPrinum(prinum,"1");
            } else {
                row = 0;
            }
        }
        return -1 != row;
    }
}